package me.shurufa.activities;

import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GestureDetectorCompat;
import android.text.TextUtils;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;

import butterknife.Bind;
import me.shurufa.R;
import me.shurufa.fragments.UserCommentAllFragment;
import me.shurufa.model.User;
import me.shurufa.utils.Constants;
import me.shurufa.widget.CircleImageView;
import me.shurufa.widget.CollapsingAvatarToolbar;

/**
 * 用户的全部评价列表
 *
 * @FileName: me.shurufa.activities.UserCommentActivity.java
 * @author: kHRYSTAL
 * @email: 723526676@qq.com
 * @date: 2016-01-27 21:46
 */
public class UserCommentActivity extends BaseActivity implements AppBarLayout.OnOffsetChangedListener, GestureDetector.OnGestureListener, View.OnTouchListener {

    @Bind(R.id.left_text)
    TextView leftText;
    @Bind(R.id.right_image)
    ImageView rightImage;
    @Bind(R.id.right_text)
    TextView rightText;
    @Bind(R.id.user_avatar)
    CircleImageView userAvatar;
    @Bind(R.id.user_name)
    TextView userName;
    @Bind(R.id.user_desc)
    TextView userDesc;
    @Bind(R.id.stuff_container)
    CollapsingAvatarToolbar stuffContainer;
    @Bind(R.id.collapsing_toolbar)
    CollapsingToolbarLayout collapsingToolbar;
    @Bind(R.id.appbar)
    AppBarLayout appBarLayout;
    @Bind(R.id.main_content)
    CoordinatorLayout mainContent;
    private User mUser;

    GestureDetectorCompat mDetector;
    int maxScrollOffset;
    private UserCommentAllFragment fragment;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        activity_layout_res = R.layout.activity_user_comment;
        super.onCreate(savedInstanceState);
        mUser = new Gson()
                .fromJson(getIntent().getStringExtra(Constants.EXTRA_USER_PARAM), User.class);
        if (mUser == null)
            return;
        uiInit();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (appBarLayout != null)
            appBarLayout.addOnOffsetChangedListener(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        if (appBarLayout != null)
            appBarLayout.removeOnOffsetChangedListener(this);
    }

    private void uiInit() {
        rightImage.setImageResource(R.drawable.ic_talk);
        rightImage.setVisibility(View.GONE);
        if (!TextUtils.isEmpty(mUser.avatar)) {
            Glide.with(this).load(mUser.avatar).into(userAvatar);
        } else {
            userAvatar.setImageResource(R.drawable.default_avatar);
        }
        if (!TextUtils.isEmpty(mUser.nickname)) {
            userName.setText(mUser.nickname);
        }
        if (!TextUtils.isEmpty(mUser.description)) {
            userDesc.setText(mUser.description);
        }

        collapsingToolbar.setOnTouchListener(this);
        mDetector = new GestureDetectorCompat(this, this);
        maxScrollOffset = appBarLayout.getTotalScrollRange();

        initFragment();
    }

    private void initFragment() {
        fragment = UserCommentAllFragment.newInstance(mUser.uid);
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.add(R.id.user_comment_container, fragment);
        transaction.commit();
    }

    @Override
    public boolean onDown(MotionEvent e) {
        int[] l = new int[2];
        appBarLayout.getLocationOnScreen(l);
        int x = l[0];
        int y = l[1];
        int w = appBarLayout.getWidth();
        int h = appBarLayout.getHeight();
        int rx = (int) e.getRawX();
        int ry = (int) e.getRawY();
        if (rx < x || rx > x + w || ry < y || ry > y + h) {
            return false;
        }
        return true;
    }

    @Override
    public void onShowPress(MotionEvent e) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        scrollAppBar((int) distanceY);
        return true;
    }

    @Override
    public void onLongPress(MotionEvent e) {

    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        return false;
    }

    void scrollAppBar(int y) {
        CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams();
        AppBarLayout.Behavior behavior = (AppBarLayout.Behavior) params.getBehavior();
        int offset = behavior.getTopAndBottomOffset() - y;
        if (offset > 0)
            offset = 0;
        if (maxScrollOffset == 0)
            maxScrollOffset = appBarLayout.getTotalScrollRange();
        if (offset < -maxScrollOffset)
            offset = -maxScrollOffset;
        behavior.setTopAndBottomOffset(offset);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return mDetector.onTouchEvent(event);
    }

    @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
        if (fragment!=null){
            fragment.enableRefresh(verticalOffset==0);
        }
    }
}
